home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / Print / Postscript / BBFig / BBFig.PS next >
Encoding:
Text File  |  1991-08-30  |  4.9 KB  |  315 lines  |  [TEXT/ttxt]

  1. %!
  2. % bbfig.ps --
  3. %
  4. % Prints a file, but keeps track of bounding box info, and prints the box at
  5. % the end (around the figure.)
  6. %
  7.  
  8. /$BoundingBox 50 dict def
  9.  
  10. $BoundingBox begin
  11. /bb$_llx 99999 def
  12. /bb$_lly 99999 def
  13. /bb$_urx -99999 def
  14. /bb$_ury -99999 def
  15.  
  16. /bb$_mtx matrix defaultmatrix def
  17. end
  18.  
  19. /bb$_xdef {
  20.     exch def
  21. } def
  22.  
  23. /bb$_put {
  24.     exch $BoundingBox 3 1 roll put
  25. } def
  26.  
  27. /bb$_pt+ {
  28.     exch
  29.     4 -1 roll add
  30.     3 1 roll add
  31. } def
  32.  
  33. %
  34. % Stubs of old functions.
  35. %
  36.  
  37. /-stroke /stroke load def
  38. /-fill /fill load def
  39. /-eofill /eofill load def
  40. /-image /image load def
  41. /-show /show load def
  42. /-awidthshow /awidthshow load def
  43. /-showpage /showpage load def
  44. /-restore /restore load def
  45.  
  46. %
  47. % New Functions.
  48. %
  49.  
  50. % make sure we remember bb around save-restores
  51. /restore {
  52.     $BoundingBox begin bb$_ury bb$_urx bb$_lly bb$_llx end
  53.     5 -1 roll
  54.     -restore
  55.     $BoundingBox begin
  56.     /bb$_llx bb$_put
  57.     /bb$_lly bb$_put
  58.     /bb$_urx bb$_put
  59.     /bb$_ury bb$_put
  60.     end
  61. } def
  62.  
  63. /stroke {
  64.     gsave
  65.     {
  66.         flattenpath
  67.          strokepath    % Make sure to take line width into account.
  68.     } stopped {        % strokepath often hits a limitcheck.
  69.         grestore    % Restore the original path
  70.         gsave
  71.     } if
  72.     bb$_path            % Accumulate it into our box.
  73.     grestore
  74.  
  75.     -stroke
  76. } def
  77.  
  78. /fill {
  79.     gsave
  80.     bb$_path
  81.     grestore
  82.  
  83.     -fill
  84. } def
  85.  
  86. /eofill {
  87.     gsave
  88.     bb$_path
  89.     grestore
  90.  
  91.     -eofill
  92. } def
  93.  
  94. %
  95. % Text is implemented by reducing everything to an `awidthshow'.
  96. %
  97.  
  98. /show {
  99.     0 0 0 0 0        % Extra parameters for awidthshow
  100.     6 -1 roll        % Bring the string back up
  101.     awidthshow
  102. } def
  103.  
  104. /widthshow {
  105.     0 0            % Extra parameters for awidthshow
  106.     3 -1 roll        % Bring the string back up.
  107.     awidthshow
  108. } def
  109.  
  110. /ashow {
  111.     0 0 0
  112.     6 3 roll
  113.     awidthshow
  114. } def
  115.  
  116. /awidthshow {
  117.     5 dict begin
  118.     gsave
  119.     currentpoint
  120.     2 copy /@starty bb$_xdef /@startx bb$_xdef
  121.     2 index stringwidth    % Get the natural length of the string
  122.     bb$_pt+            % Add to the start to get the end.
  123.  
  124.     2 index length        % How many characters?
  125.  
  126.     dup            % Add the offsets to each character
  127.     6 index mul
  128.     exch 5 index mul
  129.     bb$_pt+
  130.  
  131.     5 index 3 index
  132.     bb$_chcount        % How many padding characters?
  133.  
  134.     dup            % Add the offsets for each pad.
  135.     9 index mul
  136.     exch 8 index mul
  137.     bb$_pt+
  138.  
  139.     /@endy bb$_xdef /@endx bb$_xdef
  140.  
  141.     bb$_fontheight        % Get the height and depth of the current font.
  142.     
  143.     4 copy
  144.     @startx @starty bb$_pt+ bb$_point
  145.     @startx @starty bb$_pt+ bb$_point
  146.     @endx @endy bb$_pt+ bb$_point
  147.     @endx @endy bb$_pt+ bb$_point
  148.  
  149.     grestore
  150.     end
  151.  
  152.     -awidthshow
  153. } def
  154.  
  155. %
  156. % `image':
  157. %
  158. % Assume here that the image lands in the unit square.
  159. %
  160.  
  161. /image {
  162.     0 0 bb$_point
  163.     1 1 bb$_point
  164.  
  165.     -image
  166. } def
  167.  
  168. %
  169. % `showpage':
  170. %
  171. % Just draw the box around the figure and print the page, and then initialize
  172. % the bounding box variables again.
  173. %
  174.  
  175. /bb$_string 10 string def
  176.  
  177. /showpage {
  178.     $BoundingBox begin
  179.     initgraphics bb$_mtx setmatrix
  180.  
  181.     % Convert to default coords.
  182.     bb$_llx bb$_lly itransform
  183.     bb$_urx bb$_ury itransform
  184.     /bb$_lly bb$_put /bb$_urx bb$_put
  185.     /bb$_ury bb$_put /bb$_llx bb$_put
  186.  
  187.     bb$_llx bb$_lly moveto        % Make the box
  188.     bb$_llx bb$_ury lineto
  189.     bb$_urx bb$_ury lineto
  190.     bb$_urx bb$_lly lineto
  191.     closepath
  192.  
  193.     bb$_bwstroke            % Draw the box.
  194.  
  195.     0 setgray
  196.     /Courier findfont 10 scalefont setfont
  197.     bb$_llx bb$_lly 12 sub moveto
  198.     bb$_llx bb$_string cvs -show ( ) -show
  199.     bb$_lly bb$_string cvs -show ( ) -show
  200.     bb$_urx bb$_string cvs -show ( ) -show
  201.     bb$_ury bb$_string cvs -show
  202.  
  203.     (%%BoundingBox: ) print
  204.     bb$_llx bb$_string cvs print ( ) print
  205.     bb$_lly bb$_string cvs print ( ) print
  206.     bb$_urx bb$_string cvs print ( ) print
  207.     bb$_ury bb$_string cvs print (\n) print
  208.  
  209.     end
  210.     99999 /bb$_llx bb$_put
  211.     99999 /bb$_lly bb$_put
  212.     -99999 /bb$_urx bb$_put
  213.     -99999 /bb$_ury bb$_put
  214.     -showpage
  215. } def
  216.  
  217. %/copypage {showpage} def
  218.  
  219. %
  220. % BoundingBox functions:
  221. %
  222. % We accumulate the information about the bounding box into four variables.
  223. % The data is stored in device coordinates.
  224. %
  225.  
  226. %
  227. % x y `bb$_point' -
  228. %
  229. % Takes a point in user coordinates and adds it into the bounding box info.
  230. %
  231. /bb$_point {
  232.     $BoundingBox begin
  233.     transform        % Convert to device coords.
  234.  
  235.     dup bb$_lly lt {dup /bb$_lly bb$_put}if
  236.     dup bb$_ury gt {dup /bb$_ury bb$_put}if
  237.     pop
  238.     dup bb$_llx lt {dup /bb$_llx bb$_put}if
  239.     dup bb$_urx gt {dup /bb$_urx bb$_put}if
  240.     pop
  241.  
  242.     end
  243. } def
  244.  
  245. %
  246. % - `bb$_path' -
  247. %
  248. % Incorporates the bounding box of the path into the bounding box info.
  249. %
  250.  
  251. /bb$_path {
  252.     gsave
  253.     {
  254.         0 setlinejoin
  255.         flattenpath
  256.     } stopped {
  257.         grestore
  258.         gsave
  259.     } if
  260.     pathbbox
  261.     bb$_point
  262.     bb$_point
  263.     grestore
  264. } def
  265.  
  266. %
  267. % Stuff for text.
  268. %
  269.  
  270. %
  271. % char-code string `bb$_chcount' occurs
  272. %
  273. % Counts the number of times a character appears in a string.
  274. %
  275.  
  276. /bb$_chcount {
  277.     0 exch
  278.     {
  279.         2 index eq {
  280.             1 add
  281.         } if
  282.     } forall
  283.     exch pop
  284. } def
  285.  
  286. %
  287. % - `bb$_fontheight' heightx heighty depthx depthy
  288. %
  289. % Returns the offsets to the lowest point and highest point in the current
  290. % font.
  291. %
  292.  
  293. /bb$_fontheight {
  294.     currentfont begin
  295.     FontBBox
  296.     exch pop 0 exch
  297.     FontMatrix transform
  298.     4 2 roll
  299.     exch pop 0 exch
  300.     FontMatrix transform
  301.     end
  302. } def
  303.  
  304. /bb$_bwstroke {
  305.     0 setlinewidth
  306.     1 setgray
  307.     [5] 0 setdash
  308.     gsave -stroke grestore
  309.     0 setgray
  310.     [5] 5 setdash
  311.     -stroke
  312. } def
  313.  
  314. %  end of bb.ps
  315.